package com.kqb.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kqb.common.curd.impl.BaseServiceImpl;
import com.kqb.common.domain.page.PageResult;
import com.kqb.common.utils.Assert;

import com.kqb.mapper.ContactMapper;
import com.kqb.mapper.PaymentinfoMapper;
import com.kqb.mapper.SupplierMapper;
import com.kqb.mapper.SupplierUserMapper;
import com.kqb.pojo.User;
import com.kqb.pojo.UserType;
import com.kqb.pojo.dto.SupplierDto;
import com.kqb.query.SupplierQuery;
import com.kqb.service.SupplierService;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class SupplierServiceImpl extends BaseServiceImpl<SupplierDto, SupplierMapper> implements SupplierService {

    @Autowired
    SupplierUserMapper supplierUserMapper;


    @Autowired
    PaymentinfoMapper paymentinfoMapper;

    @Autowired
    SupplierMapper supplierMapper;

    @Autowired
    ContactMapper contactMapper;

    @Autowired
    SupplierMapper iBaseMapper;

    /**
     * 保存供应商
     *
     * @param entity
     * @return
     */
    @Override
    @Transactional
    public int save(SupplierDto entity) {
        User user = new User(entity.getAccount(), entity.getPassword(), UserType.SUPPLIER_USER);
        int save = this.supplierUserMapper.save(user);
        paymentinfoMapper.save(entity);
        contactMapper.save(entity);
        Assert.isZero(save, "保存失败!");
        return super.save(entity);
    }

    /**
     * 删除供应商
     *
     * @param id
     * @return
     */
    @Override
    @Transactional
    public int delete(Serializable id) {

        int delete = this.supplierUserMapper.delete(id);
        contactMapper.delete(id);
        paymentinfoMapper.delete(id);
        supplierMapper.delete(id);

        Assert.isZero(delete, "删除失败");
        return super.delete(id);
    }

    /**
     * 根据map查询
     *
     * @param map
     * @return
     */
    @Override
    public PageResult queryByMap(Map map) {
        int pageNum = 1;
        int pageSize = 10;
        if (map.containsKey("pageNum")) {
            pageNum = Integer.parseInt(map.get("pageNum").toString());
        }
        if (map.containsKey("pageSize")) {
            pageSize = Integer.parseInt(map.get("pageSize").toString());
        }
        PageHelper.startPage(pageNum, pageSize);
        List list = iBaseMapper.queryByMap(map);
        PageInfo info = new PageInfo(list);
        return new PageResult(pageNum, pageSize, info.getTotal(), list);
    }

    /**
     * 分页查询
     *
     * @param query
     * @return
     */
    @Override
    public PageResult query(SupplierQuery query) {
        //分页查询
        PageHelper.startPage(query.getPageNum(), query.getPageSize());//后面紧跟查询
        List<Map> list = iBaseMapper.query(query);
        PageInfo info = new PageInfo(list);
        return new PageResult(query.getPageNum(), query.getPageSize(), info.getTotal(), list);
    }
}
